package xyz.hzxiao.hello.controller;

import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * SayHello控制器
 *
 * @author ：FuXx
 * @className : xyz.hzxiao.hello.controller.SayHelloController
 * @since ：2025/7/31
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/hello")
public class SayHelloController {

    private final StreamingChatLanguageModel chatLanguageModel;
//    private final ChatLanguageModel chatLanguageModel;


    @GetMapping(value = "/chat")
    public Flux<String> sayHello(@RequestParam(value = "message", defaultValue = "FuXx") String message, HttpServletResponse response) {
        return Flux.create(sink -> chatLanguageModel.chat(message, new StreamingChatResponseHandler() {
            @Override
            public void onPartialResponse(String s) {
                log.info("partial=====>{}", s);
                sink.next(s);
            }

            @Override
            public void onCompleteResponse(ChatResponse chatResponse) {
                sink.complete();
            }

            @Override
            public void onError(Throwable throwable) {
                log.error("error=====>{}", throwable.getMessage());
                sink.error(throwable);
            }
        }));
//        return chatLanguageModel.chat(message);
    }
}
